package edu.gatech.cs2340.group7.wheresmystuffapp.views;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import edu.gatech.cs2340.group7.wheresmystuffapp.R;
import edu.gatech.cs2340.group7.wheresmystuffapp.presenters.CreateAdminPresenter;

/**
 * View for creating an admin account
 * 
 * @author tbowling3
 * 
 */
public class CreateAdminView extends Activity implements View.OnClickListener {
	private String PACKAGE_NAME;
	private CreateAdminPresenter myPresenter;
	private EditText etAdminEmail;
	private EditText etAdminPassword;
	private EditText etAdminPasswordConfirm;
	private EditText etAdminPhone;
	private TextView tvCreationResult;
	private Button bCreateAdmin;
	private Button bCancel;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_create_admin);
		initialize();
		PACKAGE_NAME = getApplicationContext().getPackageName();
		myPresenter = new CreateAdminPresenter(this);
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.b_admin_submit_creation:
			String email = etAdminEmail.getText().toString();
			String password = etAdminPassword.getText().toString();
			String passwordConfirm = etAdminPasswordConfirm.getText()
					.toString();
			String phone = etAdminPhone.getText().toString();
			myPresenter.createAdmin(email, password, passwordConfirm, phone);
			tvCreationResult.setText("Admin Created");
			tvCreationResult.setTextColor(Color.RED);
			break;
		case R.id.b_admin_cancel_from_create:
			Intent intent = new Intent(this, AdminMainMenuView.class);
			intent.setPackage(PACKAGE_NAME);
			startActivity(intent);
			break;
		}

	}

	/**
	 * Sets the text to be shown for the Result Text View on the XML layout
	 * 
	 * @param result
	 *            String to be set on the Result text view
	 */
	public void setResultText(String result) {
		if (result != null) {
			if (!result.isEmpty()) {
				tvCreationResult.setText(result);
				tvCreationResult.setTextColor(Color.RED);
			}
		}
	}

	/**
	 * Finds and sets the links to eat of the buttons, text views, and edit
	 * texts in the XML layout
	 */
	private void initialize() {
		etAdminEmail = (EditText) findViewById(R.id.et_admin_email);
		etAdminPassword = (EditText) findViewById(R.id.et_admin_create_password);
		etAdminPasswordConfirm = (EditText) findViewById(R.id.et_admin_create_password_confirm);
		etAdminPhone = (EditText) findViewById(R.id.et_admin_create_phone);
		tvCreationResult = (TextView) findViewById(R.id.tv_admin_creation_result);
		bCreateAdmin = (Button) findViewById(R.id.b_admin_submit_creation);
		bCancel = (Button) findViewById(R.id.b_admin_cancel_from_create);

		bCreateAdmin.setOnClickListener(this);
		bCancel.setOnClickListener(this);
	}
}
